package com.java.forum.d_controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.forum.a_entity.*;
import com.java.forum.a_entity.Collection;
import com.java.forum.c_service.CommentService;
import com.java.forum.c_service.PostService;
import com.java.forum.c_service.UserService;
import com.java.forum.e_util.JsonResult;
import com.java.forum.h_annotation.CurrentUser;
import com.java.forum.h_annotation.NeedToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
@RestController
@RequestMapping("post")
public class PostController extends BaseController {
    private PostService postService;
    private CommentService commentService;
    private UserService userService;

    @Scheduled(fixedRate = 30 * 60 * 1000)
    private void work() {
        List<Post> posts = postService.getAllPost(new Date());
        for (Post p : posts) {
            long date0 = p.getDate().getTime();
            long date1 = new Date().getTime();
            Integer likes = p.getLikes();
            Integer collections = p.getCollections();
            Integer views = p.getViews();
            if (views == null) {
                p.setViews(0);
                views = 0;
            }
            double s = Math.log((100000.0 / (date1 - date0)) * 60 * 60 * 6.0 * 0.85) + likes * 0.9 + collections * 1.1 + views * 0.15;
            p.setScore(s);
            postService.reCounterScore(p);
        }
    }

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @NeedToken
    @RequestMapping("PublishPost")
    public JsonResult<Map<String, Integer>> PublishPost(Post post, @CurrentUser User user) {
        post.setUid(user.getUid());
        Integer res = postService.publishPost(post);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("pid", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("postInfo")
    public JsonResult<Map<String, Object>> getPostInfo(Integer pid, @CurrentUser User user) {
        Post res = postService.getPostInfo(pid);
        if (res.getUser() == null) {
            res.setUser(userService.getUserInfo(res.getUid()));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("post", res);
        //map.put() //是否点赞
        return new JsonResult<>(ok, map);

    }

    @NeedToken
    @RequestMapping("getPostListByType")
    public JsonResult<List<Post>> getPostListByType(Integer type, long date) {
        Date d = new Date(date);
        PageHelper.startPage(1, 10);
        List<Post> list = null;
        if (type == -1) {
            list = postService.getAllPost(d);
        } else {
            list = postService.getPostByType(type, d);
        }
        for (Post p : list) {
            if (p.getUser() == null) {
                p.setUser(userService.getUserInfo(p.getUid()));
            }
        }
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        JsonResult<List<Post>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("DeletePost")
    public JsonResult<Void> DeletePost(Post post) {
        postService.deletePost(post);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("TheNew")
    public JsonResult<List<Post>> TheNew(@CurrentUser User user, long date, Integer type) {
        Date d = new Date(date);
        PageHelper.startPage(1, 10);
        List<Subscribe> my_sub = userService.getSubscribeList(user.getUid());
        List<Integer> BUid = my_sub.stream().map(Subscribe::getBUid).collect(Collectors.toList());
        List<Post> posts = null;
        if(type == 1)
            posts = postService.getAllPost(d);
        else if(type == 2)
            posts = postService.getAllPostOrderByComments(d);
        else if(type == 3)
            posts = postService.getAllPostOrderByLikes(d);
        else
            posts = postService.getAllPostOrderByViews(d);
        List<Post> list = new ArrayList<>();
        for (Post p : posts) {
            if (BUid.contains(p.getUid())) {
                if (p.getUser() == null) {
                    p.setUser(userService.getUserInfo(p.getUid()));
                }
                list.add(p);
            }
        }
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        JsonResult<List<Post>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("recommendation")
    public JsonResult<List<Post>> recommendation(long date) {//推荐
        Date d = new Date(date);
        PageHelper.startPage(1, 100);
        List<Post> list = postService.getAllPost(d);
        list.sort((o1, o2) -> (int) (o2.getScore() - o1.getScore()));
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        for (Post p : list) {
            if (p.getUser() == null) {
                p.setUser(userService.getUserInfo(p.getUid()));
            }
        }
        JsonResult<List<Post>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("getPostListByKeyword")
    public JsonResult<List<Post>> getPostListByKeyword(String keyword, long date) {
        Date d = new Date(date);
        PageHelper.startPage(1, 10);
        List<Post> list = postService.getPostByKeyword(keyword, d);
        for (Post p : list) {
            if (p.getUser() == null) {
                p.setUser(userService.getUserInfo(p.getUid()));
            }
        }
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        JsonResult<List<Post>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("getPostListByUid")
    public JsonResult<List<Post>> getPostListByUid(Integer uid, long date) {
        Date d = new Date(date);
        PageHelper.startPage(1, 10);
        List<Post> list = postService.getPostByUid(uid, d);
        PageInfo<Post> pageInfo = new PageInfo<>(list);
        JsonResult<List<Post>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }


    @NeedToken
    @RequestMapping("likePost")
    public JsonResult<Map<String, Integer>> likePost(PLike pLike, @CurrentUser User user) {
        pLike.setUid(user.getUid());
        Integer res = postService.likePost(pLike);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("id", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("UnLikePost")
    public JsonResult<Void> UnLikePost(PLike pLike, @CurrentUser User user) {
        pLike.setUid(user.getUid());
        postService.UnLikePost(pLike);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("isLiked")
    public JsonResult<Map<String, Integer>> isLiked(int pid, @CurrentUser User user) {
        int uid = user.getUid();
        Integer res = postService.isLiked(pid, uid);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("isLike", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("ReEditPost")
    public JsonResult<Map<String, Integer>> ReEditPost(Post post, @CurrentUser User user) {
        Post cur = postService.getPostInfo(post.getPid());
        if (!cur.getUid().equals(user.getUid())) {
            return new JsonResult<>(err, "没有修改权限!");//如果无权更改，就直接返回空
        }
        Integer res = postService.ReEditPost(post);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("pid", res);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("commentPost")
    public JsonResult<PComment> commentPost(PComment pComment, @CurrentUser User user) {
        pComment.setUid(user.getUid());
        PComment res = commentService.commentPost(pComment);
        return new JsonResult<>(ok, res);
    }

    @NeedToken
    @RequestMapping("getCommentInfo")
    public JsonResult<Map<String, Object>> getCommentInfo(Integer cid, @CurrentUser User user) {
        PComment res = commentService.getCommentInfo(cid);
        HashMap<String, Object> map = new HashMap<>();
        map.put("Comment", res);
        return new JsonResult<>(ok, map);

    }

    @NeedToken
    @RequestMapping("getCommentByPid")
    public JsonResult<List<PComment>> getCommentByPid(Integer pid, Integer startCid) {
        PageHelper.startPage(1, 8);
        List<PComment> list = commentService.getCommentByPid(pid, startCid);
        PageInfo<PComment> pageInfo = new PageInfo<>(list);
        JsonResult<List<PComment>> jsonResult = new JsonResult<>();
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setState(ok);
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("DeleteComment")
    public JsonResult<Void> DeleteComment(PComment pComment) {
        commentService.DeleteComment(pComment);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("getCCommentByCid")
    public JsonResult<List<CComment>> getCCommentByCid(Integer cid, Integer startCcid) {
        PageHelper.startPage(1, 8);
        List<CComment> CC_List = commentService.CCommentList(cid, startCcid);
        PageInfo<CComment> pageInfo = new PageInfo<>(CC_List);
        JsonResult<List<CComment>> jsonResult = new JsonResult<>();
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setState(ok);
        jsonResult.setData(CC_List);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("CommentComment")
    public JsonResult<Map<String, Integer>> CommentComment(CComment cComment, @CurrentUser User user) {
        cComment.setUid(user.getUid());
        Integer res = commentService.commentComment(cComment);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("ccid", res);
        return new JsonResult<>(ok, map);
    }
}
